home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: What's better & why
- Date: Tue, 20 Feb 1996 11:33:11 +0200
- Organization: Carelcomp Forest
- Message-ID: <31299557.3861@cmt.lpr.mail.carel.fi>
- References: <31297C5A.E6C@connix.com>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Scott Hawley wrote:
- >
- > I was just curious what you all though about the following code.
- > Please tell me what is better (faster/Smaller) and why. Or does it make
- > any difference at all. I just though about this while I was programming
- > today?
- >
- > example 1:
- > val = 1;
- > if( what ever...)val = 0;
-
- This might be compiled to (expressed in Assembly):
-
- mov [val],1
- ; perform comparison
- jz Skip ; or the proper flag (jnz, jc...)
- mov [val],0
- Skip: ; other code
-
- >
- > or
- > example 2:
- > if( what ever... )val = 0;
- > else val = 1;
- >
-
- This might become:
-
- ; perform comparison
- jz Skip ; or the proper flag
- mov [val],0
- jmp Skip2
- Skip: mov [val],1
- Skip2: ; other code
-
- > amy remarks?
-
- So, it seems the latter produces more code. However, a smart compiler would probably
- optimize it to be something like the first example. In any case, I normally use:
-
- val = (what ever) ? 0 : 1;
-
- Which you'd prefer, that's up to you. An optimizing compiler could even produce code
- like this:
-
- mov [val],0
- ; perform comparison
- jnz Skip ; oppose result from the ones above, val left as 0
- inc [val] ; val = val + 1, ie. 1
- Skip: ; other code
-
- which would be better than the two examples above. (The examples assume use of an
- Intel-based compiler.)
-
- Later,
- AriL
- --
- All my opinions are mine and mine alone.
-